package thread;

import exception.ThrowsDemo;

/*
同步块
有效的缩小同步范围可以保证并发安全的前提下尽可能的提高并发效率
synchronized(同步监视器对象){需要多线程同步执行的代码片段}
 */
public class SyncDemo2 {
    public static void main(String[] args) {
        shop shop1 = new shop();

        Thread ti = new Thread("qianqian"){
            public void run(){
            shop1.buy();
            }
        };
        Thread to = new Thread("w"){
            public void run(){
            shop1.buy();
            }
        };
            ti.start();
            to.start();

    }
}
class shop{
    /*
     在成员方法上使用Synchronized 那么同步监视器对象就是该方法所属对象
     */
    Thread t = Thread.currentThread();
    public synchronized void buy() {
    try {
        System.out.println(t.getName()+"正在挑衣服。。。");
        Thread.sleep(5000);
        synchronized (new Object()){
        System.out.println(t.getName()+"正在试衣服");
        Thread.sleep(5000);}
        System.out.println(t.getName()+"结账走人");
    }catch (Exception e){
    }


    }
}
